package com.springcloud.order.controller;

import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import com.hyr.springcloud.dto.OrderDTO;
import com.hyr.springcloud.service.IOrderService;
import com.springcloud.order.demain.OrderInfo;
import com.springcloud.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/order")
public class OrderController implements IOrderService {

    @Autowired
    private OrderService orderService;


    @Autowired
    private JmsTemplate jmsTemplate;

    private TimeBasedGenerator uuidGenerator = Generators.timeBasedGenerator();

    @RequestMapping("/create")
    public String create(OrderDTO orderDTO){
        orderDTO.setToken(uuidGenerator.generate().toString());
        jmsTemplate.convertAndSend("order:new",orderDTO);
        return "OK";
    }



    @RequestMapping("/getOrderByUuid/{token}")
    public OrderInfo getOrderByUuid(@PathVariable(value = "token") String token){
        return orderService.getOrder(token);
    }


    @RequestMapping("/getOrder/{id}")
    public OrderDTO getOrder(@PathVariable(value = "id") int id){
       return null;
    }


}
